package com.central.operate.service.impl;

import com.central.model.common.PageResult;
import com.central.model.common.utils.PageUtil;
import com.central.operate.dao.LoginCountDao;
import com.central.operate.dto.ChartParams;
import com.central.operate.model.LoginInfo;
import com.central.operate.model.OperateRecord;
import com.central.operate.model.RecordTime;
import com.central.operate.service.LoginCountService;
import com.central.operate.vo.LoginCountVO;
import com.central.operate.vo.RegisterCountChartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class LoginCountServiceImpl implements LoginCountService {

    @Autowired
    private LoginCountDao loginCountDao;

//    @Override
//    public PageResult<RecordTime> listLoginCount(Map<String, Object> params) {
//        List<RecordTime> recordByH = loginCountDao.recordByH(params);
//        return PageResult.<RecordTime>builder().data(recordByH).code(0).count((long)recordByH.size()).build();
//    }

    @Override
    public PageResult<LoginCountVO> listLoginCount(String time) {
        List<OperateRecord> loginCounts = loginCountDao.listLoginCountByDay(time);
        List<LoginCountVO> loginCountVOList = new ArrayList<>();
        loginCounts.forEach(loginCount ->{
            LoginCountVO loginCountVO = new LoginCountVO(Integer.valueOf(loginCount.getStartTime().split(" ")[1].split(":")[0]),loginCount.getLogin());
            loginCountVOList.add(loginCountVO);
        });
        return PageResult.<LoginCountVO>builder().data(loginCountVOList).code(0).count((long)loginCountVOList.size()).build();
    }

    @Override
    public PageResult<LoginInfo> listOnline(Map<String, Object> params) {
        PageUtil.pageParamConver(params,true);
        int total = loginCountDao.countOnline(params);
        List<LoginInfo> list = Collections.emptyList();
        if (total > 0){
            list = loginCountDao.findOnlineList(params);
        }
        return PageResult.<LoginInfo>builder().data(list).code(0).count((long)total).build();
    }

    @Override
    public Integer countOnlineHistory(Map<String, Object> params) {
        return loginCountDao.countOnlineHistory(params);
    }

    @Override
    public Integer countLoginCountHistory(Map<String, Object> params) {
        return loginCountDao.countLoginHistory(params);
    }

    @Override
    public List<RegisterCountChartVO> listLoginCount(Map<String, Object> params) {
//        if (params.get("start_time") == null || params.get("start_time") == "") {
//            Date date = new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            params.put("start_time",sdf.format(date));
//        }
//        if (params.get("consecutive_day") == null || params.get("consecutive_day") == ""){
//            params.put("consecutive_day","7");
//        }
//        if (params.get("interval_hour") == null || params.get("interval_hour") == ""){
//            params.put("interval_hour","1");
//        }
        ChartParams chartParams = new ChartParams(params);
        log.info(chartParams.toString());
        return loginCountDao.listLogin(chartParams);
//        return loginCountDao.listLogin(params);
    }

    @Override
    public List<RegisterCountChartVO> listOnlineCount(Map<String, Object> params) {
//        if (params.get("start_time") == null || params.get("start_time") == "") {
//            Date date = new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            params.put("start_time",sdf.format(date));
//        }
//        if (params.get("consecutive_day") == null || params.get("consecutive_day") == ""){
//            params.put("consecutive_day","7");
//        }
//        if (params.get("interval_hour") == null || params.get("interval_hour") == ""){
//            params.put("interval_hour","1");
//        }
        ChartParams chartParams = new ChartParams(params);
        log.info(chartParams.toString());
        return loginCountDao.listOnline(chartParams);
//        return loginCountDao.listOnline(params);
    }
}
